package com.pengcheng.hnweb.util;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    public boolean set(String key,String val,long expire)
    {
        if(expire>0)
        {
            stringRedisTemplate.opsForValue().set(key,val);
            stringRedisTemplate.expire(key,expire,TimeUnit.MILLISECONDS);
        }
        else {
            stringRedisTemplate.opsForValue().set(key,val);
        }
        return true;
    }
    public String get(String key)
    {
        return stringRedisTemplate.opsForValue().get(key);
    }
}
